package com.atzongmaotan.reggie.controller;

import com.atzongmaotan.reggie.common.R;
import com.atzongmaotan.reggie.entity.Category;
import com.atzongmaotan.reggie.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:    CategoryController
 * Package:  com.atzongmaotan.controller
 * Description:
 *
 * @Author: interface
 * @Create: 2024/6/12 - 下午11:43
 * @Version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> saveCategory(@RequestBody Category category) {
        log.info("category:{}",category.toString());
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    @GetMapping("page")
    public R<Page> list(int page,int pageSize) {
        Page<Category> categoryPage = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //条件排序
        wrapper.orderByAsc(Category::getSort);
        //分页条件排序
        categoryPage = categoryService.page(categoryPage,wrapper);
        return R.success(categoryPage);
    }
    @DeleteMapping
    public R<String> deleteCategoryById(@RequestParam("id") Long id) {
        log.info("删除categoryId:{}",id);
        //判断category是否存在菜单或者套餐 categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除成功");
    }
    @PutMapping
    public R<String> updateCategoryById(@RequestBody Category category) {
        log.info("category:{}",category.toString());
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }
    @GetMapping("list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //添加条件
        wrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> categoryList = categoryService.list(wrapper);
        return R.success(categoryList);
    }

}
